﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperKit {
    /*
     * 这个项目的功能是将文件按照数量合并到一个目录里面。
     * 比如说这个目录下有100个文件，如果分页数量是10，那么会分成10个目录，分别存放10个文件。
     */
    class SplitFilePage : CommandExecutor {
        private class Page {
            public List<FileInfo> files;

            public Page(int pageCount) {
                files = new List<FileInfo>(pageCount);
            }

            public void Add(FileInfo f) {
                files.Add(f);
            }

            public void MoveTo(string dir) {
                foreach (var i in files) {
                    string path = Path.Combine(dir, i.Name);
                    File.Move(i.FullName, path);
                }                
            }
        }

        private const string JOIN_CHAR = "_";

        private FileInfo[] files;
        private bool[] processed;
        // 文件分离的数量
        private int pageCount;
        // 是否删除掉前后的序号
        public bool trimNumber;

        public SplitFilePage(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {
            pageCount = arg.GetInteger("-pageCount", 1);
            trimNumber = arg.IsContain("-trimNumber");
        }

        public override void Execute() {
            base.Execute();
            
            files = GetSortFiles(FilePath);
            processed = new bool[files.Length];
            for (int i = 0; i < processed.Length; i++) {
                processed[i] = false;
            }

            // 得到分页的数量
            int page = GetPage(files.Length);
            var pages = new Page[page];
            // 建立分页
            for (int i = 0; i < page; i++) {
                pages[i] = new Page(pageCount);
            }

            // 依次将文件塞入分页
            for (int i = 0; i < files.Length; i++) {
                int pageIndex = GetPage(i + 1) - 1;
                pages[pageIndex].Add(files[i]);
            }

            // 将每一个分页输出
            foreach (var i in pages) {
                var name = CombineName(i.files);
                var dir = Path.Combine(FilePath, name);
                if (!Directory.Exists(dir)) {
                    Directory.CreateDirectory(dir);
                }
                i.MoveTo(dir);
            }

            End();
        }

        public string CombineName(List<FileInfo> files) {
            // 得到第一个项目，第一个项目的序号不需要删除
            string name = Path.GetFileNameWithoutExtension(files[0].Name);
            for (int i = 1; i < files.Count; i++) {
                name += JOIN_CHAR + TrimName(files[i].Name);
            }
            return name;
        }

        private static readonly char[] TRIM_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' };
        private string TrimName(string name) {
            name = Path.GetFileNameWithoutExtension(name);
            if (trimNumber == false) {
                return name;
            } else {
                return name.Trim(TRIM_CHARS);
            }            
        }

        private int GetPage(int index) {
            return index / pageCount + (index % pageCount > 0 ? 1 : 0);
        }

        public static FileInfo[] GetSortFiles(string path) {
            DirectoryInfo dir = new DirectoryInfo(path);
            var files = dir.GetFiles();
            List<FileInfo> list = new List<FileInfo>();
            foreach (var i in files) {
                list.Add(i);
            }
            list.Sort((a, b) => {
                return a.Name.CompareTo(b.Name);
            });
            for (int i = 0; i < files.Length; i++) {
                files[i] = list[i];
            }
            return files;
        }
    }
}
